﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RipPoll.DataAccess;
using RipPoll.Library;

namespace RipPoll.Business
{
    public class CommunityBO
    {

        public CommunityPE GetCommunity(int CommunityId)
        {
            RippollEntities re = new RippollEntities();

            CommunityPE ce = (from c in re.Communities
                        where c.Id ==  CommunityId
                        select new CommunityPE ()
                        { 
                            Id = c.Id, 
                            Name = c.Name,  
                            DateAdded = c.DateAdded , 
                            DateUpdated = c.DateUpdated, 
                            IsApproved = c.IsApproved,
                            ImagePath = c.ImagePath,
                            Question  = c.Question
                        }).SingleOrDefault<CommunityPE>();
            
            return ce;
        }

        public List<CommunityPE> GetUserCommunities(int userId)
        {
            RippollEntities re = new RippollEntities();


            List<CommunityPE> cList = (from c in re.Communities
                         join uc in re.UserCommunities on c.Id equals uc.CommunityId
                         where uc.UserId == userId
                         select new CommunityPE() 
                            { 
                                Id = c.Id, 
                                Name = c.Name,
                                DateAdded = c.DateAdded, 
                                DateUpdated = c.DateUpdated, 
                                IsApproved = c.IsApproved ,
                                ImagePath = c.ImagePath,
                                Question = c.Question
                            }).ToList<CommunityPE>();

            return cList;
        }


        public string[] GetPollZipCodes(int cId)
        {
            RippollEntities re = new RippollEntities();

            IQueryable<string> zipcodes = from uc in re.UserCommunities
                                          join u in re.Users on uc.UserId equals u.Id
                                          where uc.CommunityId == cId
                                          select u.ZipCode;

            return zipcodes.ToArray<string>();
        }

        
        public void AddCommunity(CommunityPE communityPE, UserPE upe)
        {

            RippollEntities re = new RippollEntities();
            Community c = new Community() { Name = communityPE.Name, DateAdded = DateTime.Now, Question = communityPE.Question, ImagePath = communityPE.ImagePath, IsApproved = false, DateUpdated = null, AccessLevelId = communityPE.AccesslevelId };
            re.AddToCommunities(c);            
            re.SaveChanges();
            communityPE.Id = c.Id;

            User u = new User(){ Id = upe.Id, City = upe.City, BirthYear = (int)upe.BirthYear, EmailId = upe.Email, Gender = upe.Gender, State = upe.State, ZipCode = upe.ZipCode, FirstName = upe.FirstName, LastName = upe.LastName, DateAdded = upe.DateAdded, DateUpdated = upe.DateAdded , EducationBackgroundId = upe.EducationId, ScreenName = upe.ScreenName };
            UserCommunity uc = new UserCommunity() { UserId= u.Id, CommunityId = c.Id, IsCreated = true, IsTracking=true, DateAdded = DateTime.Now, DateUpdated = null};

            re.UserCommunities.AddObject(uc);            
            re.SaveChanges();
            
        }


        public List<CommunityPE> GetUnApprovedCommunities()
        {
            RippollEntities re = new RippollEntities();

            List<CommunityPE> communityPEs = new List<CommunityPE>();

            IQueryable<CommunityPE> v = from c in re.Communities
                                   where c.IsApproved == false
                                   select new CommunityPE()
                                   {
                                       Name = c.Name,
                                       Id = c.Id,
                                       ImagePath = c.ImagePath,
                                       Question = c.Question,
                                       DateAdded = c.DateAdded,
                                       DateUpdated = c.DateUpdated
                                   };

            return communityPEs;
        }


        public void ApproveCommunity(int id)
        {
            RippollEntities re = new RippollEntities();
            re.Communities.Single<Community>(c => c.Id == id).IsApproved = true;
            re.SaveChanges();

        }
    }
}
